home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch13 / RotOrder.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-20  |  10KB  |  330 lines

  1. VERSION 5.00
  2. Begin VB.Form frmRotOrder 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "RotOrder"
  6.    ClientHeight    =   5670
  7.    ClientLeft      =   825
  8.    ClientTop       =   630
  9.    ClientWidth     =   8190
  10.    BeginProperty Font 
  11.       Name            =   "MS Sans Serif"
  12.       Size            =   8.25
  13.       Charset         =   0
  14.       Weight          =   700
  15.       Underline       =   0   'False
  16.       Italic          =   0   'False
  17.       Strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    KeyPreview      =   -1  'True
  21.    LinkTopic       =   "Form1"
  22.    PaletteMode     =   1  'UseZOrder
  23.    ScaleHeight     =   5670
  24.    ScaleWidth      =   8190
  25.    Begin VB.PictureBox Proj 
  26.       AutoRedraw      =   -1  'True
  27.       Height          =   2655
  28.       Index           =   0
  29.       Left            =   0
  30.       ScaleHeight     =   -5
  31.       ScaleLeft       =   -2
  32.       ScaleMode       =   0  'User
  33.       ScaleTop        =   2
  34.       ScaleWidth      =   5
  35.       TabIndex        =   5
  36.       Top             =   3000
  37.       Width           =   2655
  38.    End
  39.    Begin VB.PictureBox Proj 
  40.       AutoRedraw      =   -1  'True
  41.       Height          =   2655
  42.       Index           =   1
  43.       Left            =   2760
  44.       ScaleHeight     =   -5
  45.       ScaleLeft       =   -2
  46.       ScaleMode       =   0  'User
  47.       ScaleTop        =   2
  48.       ScaleWidth      =   5
  49.       TabIndex        =   4
  50.       Top             =   3000
  51.       Width           =   2655
  52.    End
  53.    Begin VB.PictureBox Proj 
  54.       AutoRedraw      =   -1  'True
  55.       Height          =   2655
  56.       Index           =   2
  57.       Left            =   5520
  58.       ScaleHeight     =   -5
  59.       ScaleLeft       =   -2
  60.       ScaleMode       =   0  'User
  61.       ScaleTop        =   2
  62.       ScaleWidth      =   5
  63.       TabIndex        =   3
  64.       Top             =   3000
  65.       Width           =   2655
  66.    End
  67.    Begin VB.PictureBox Pict 
  68.       AutoRedraw      =   -1  'True
  69.       Height          =   2655
  70.       Index           =   2
  71.       Left            =   5520
  72.       ScaleHeight     =   -5
  73.       ScaleLeft       =   -2
  74.       ScaleMode       =   0  'User
  75.       ScaleTop        =   2
  76.       ScaleWidth      =   5
  77.       TabIndex        =   2
  78.       Top             =   240
  79.       Width           =   2655
  80.    End
  81.    Begin VB.PictureBox Pict 
  82.       AutoRedraw      =   -1  'True
  83.       Height          =   2655
  84.       Index           =   1
  85.       Left            =   2760
  86.       ScaleHeight     =   -5
  87.       ScaleLeft       =   -2
  88.       ScaleMode       =   0  'User
  89.       ScaleTop        =   2
  90.       ScaleWidth      =   5
  91.       TabIndex        =   1
  92.       Top             =   240
  93.       Width           =   2655
  94.    End
  95.    Begin VB.PictureBox Pict 
  96.       AutoRedraw      =   -1  'True
  97.       Height          =   2655
  98.       Index           =   0
  99.       Left            =   0
  100.       ScaleHeight     =   -5
  101.       ScaleLeft       =   -2
  102.       ScaleMode       =   0  'User
  103.       ScaleTop        =   2
  104.       ScaleWidth      =   5
  105.       TabIndex        =   0
  106.       Top             =   240
  107.       Width           =   2655
  108.    End
  109.    Begin VB.Label Label1 
  110.       Alignment       =   2  'Center
  111.       Caption         =   "Directly around a line"
  112.       Height          =   255
  113.       Index           =   2
  114.       Left            =   5520
  115.       TabIndex        =   8
  116.       Top             =   0
  117.       Width           =   2655
  118.    End
  119.    Begin VB.Label Label1 
  120.       Alignment       =   2  'Center
  121.       Caption         =   "Into X-Z plane first"
  122.       Height          =   255
  123.       Index           =   1
  124.       Left            =   2760
  125.       TabIndex        =   7
  126.       Top             =   0
  127.       Width           =   2655
  128.    End
  129.    Begin VB.Label Label1 
  130.       Alignment       =   2  'Center
  131.       Caption         =   "Into Y-Z plane first"
  132.       Height          =   255
  133.       Index           =   0
  134.       Left            =   0
  135.       TabIndex        =   6
  136.       Top             =   0
  137.       Width           =   2655
  138.    End
  139. Attribute VB_Name = "frmRotOrder"
  140. Attribute VB_GlobalNameSpace = False
  141. Attribute VB_Creatable = False
  142. Attribute VB_PredeclaredId = True
  143. Attribute VB_Exposed = False
  144. Option Explicit
  145. ' Point being rotated into the Z axis.
  146. Private Const Px = 2
  147. Private Const Py = 2
  148. Private Const Pz = 1
  149. ' Line for direct rotation.
  150. Private Const Vx = 1
  151. Private Const Vy = 1
  152. Private Const Vz = 2
  153. ' Location of viewing eye.
  154. Private EyeR As Single
  155. Private EyeTheta As Single
  156. Private EyePhi As Single
  157. ' Location of focus point.
  158. Private Const FocusX = 0#
  159. Private Const FocusY = 0#
  160. Private Const FocusZ = 0#
  161. Private Projector(1 To 4, 1 To 4) As Single
  162. ' Matrices used for the reflection.
  163. Private M1(1 To 4, 1 To 4) As Single
  164. Private M2(1 To 4, 1 To 4) As Single
  165. Private M3(1 To 4, 1 To 4) As Single
  166. Private M4(1 To 4, 1 To 4) As Single
  167. Private M5(1 To 4, 1 To 4) As Single
  168. Private Identity(1 To 4, 1 To 4) As Single
  169. Private Sub CreateMatrices()
  170. Dim sin1 As Single
  171. Dim cos1 As Single
  172. Dim sin2 As Single
  173. Dim cos2 As Single
  174. Dim d1 As Single
  175. Dim d2 As Single
  176.     m3Identity Identity
  177.     ' *************
  178.     ' * Y-Z first *
  179.     ' *************
  180.     d1 = Sqr(Px * Px + Pz * Pz)
  181.     sin1 = -Px / d1
  182.     cos1 = Pz / d1
  183.     d2 = Sqr(Px * Px + Py * Py + Pz * Pz)
  184.     sin2 = Py / d2
  185.     cos2 = d1 / d2
  186.     m3Identity M1       ' Around Y into Y-Z plane.
  187.     M1(1, 1) = cos1
  188.     M1(1, 3) = -sin1
  189.     M1(3, 1) = sin1
  190.     M1(3, 3) = cos1
  191.     m3Identity M2       ' Around X into Z axis.
  192.     M2(2, 2) = cos2
  193.     M2(2, 3) = sin2
  194.     M2(3, 2) = -sin2
  195.     M2(3, 3) = cos2
  196.         
  197.     ' *************
  198.     ' * X-Z first *
  199.     ' *************
  200.     d1 = Sqr(Py * Py + Pz * Pz)
  201.     sin1 = Py / d1
  202.     cos1 = Pz / d1
  203.     d2 = Sqr(Px * Px + Py * Py + Pz * Pz)
  204.     sin2 = -Px / d2
  205.     cos2 = d1 / d2
  206.     m3Identity M3       ' Around X into X-Z plane.
  207.     M3(2, 2) = cos1
  208.     M3(2, 3) = sin1
  209.     M3(3, 2) = -sin1
  210.     M3(3, 3) = cos1
  211.     m3Identity M4       ' Around Y into Z axis.
  212.     M4(1, 1) = cos2
  213.     M4(1, 3) = -sin2
  214.     M4(3, 1) = sin2
  215.     M4(3, 3) = cos2
  216.     ' ***************
  217.     ' * Around line *
  218.     ' ***************
  219.     m3LineRotate M5, 0, 0, 0, Vx, Vy, Vz, PI
  220. End Sub
  221. ' Let the user change the location of the eye.
  222. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  223. Const Dtheta = PI / 20
  224.     Select Case KeyCode
  225.         Case vbKeyLeft
  226.             EyeTheta = EyeTheta - Dtheta
  227.             
  228.         Case vbKeyRight
  229.             EyeTheta = EyeTheta + Dtheta
  230.         
  231.         Case vbKeyUp
  232.             EyePhi = EyePhi - Dtheta
  233.         
  234.         Case vbKeyDown
  235.             EyePhi = EyePhi + Dtheta
  236.         
  237.         Case Else
  238.             Exit Sub
  239.     End Select
  240.     ' Redraw the pictures.
  241.     DrawTheData
  242. End Sub
  243. Private Sub Form_Load()
  244.     ' Initialize the eye position.
  245.     EyeR = 3
  246.     EyeTheta = PI * 0.4
  247.     EyePhi = PI * 0.1
  248.     ' Create the rotation matrices.
  249.     CreateMatrices
  250.     ' Create, project, and draw the data.
  251.     DrawTheData
  252. End Sub
  253. ' Draw all the pictures.
  254. Private Sub DrawTheData()
  255. Dim i As Integer
  256.     ' Compute the projection matrix.
  257.     m3PProject Projector, m3Parallel, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  258.     ' ***********************
  259.     ' * Around Y axis first *
  260.     ' ***********************
  261.     CreateData
  262.     TransformAllData Projector
  263.     DrawSomeData Pict(0), 1, 3, vbRed, True
  264.     DrawSomeData Pict(0), 5, NumSegments, ForeColor, False
  265.     TransformData M1, 5, NumSegments
  266.     SetPoints 5, NumSegments
  267.     TransformData Projector, 5, NumSegments
  268.     DrawSomeData Pict(0), 5, NumSegments, ForeColor, False
  269.     TransformData M2, 5, NumSegments
  270.     SetPoints 5, NumSegments
  271.     TransformData Projector, 5, NumSegments
  272.     DrawSomeData Pict(0), 5, NumSegments, ForeColor, False
  273.     TransformAllData Identity
  274.     DrawSomeData Proj(0), 1, 3, vbRed, True
  275.     DrawSomeData Proj(0), 5, NumSegments, ForeColor, False
  276.     ' ***********************
  277.     ' * Around X axis first *
  278.     ' ***********************
  279.     CreateData
  280.     TransformAllData Projector
  281.     DrawSomeData Pict(1), 1, 3, vbRed, True
  282.     DrawSomeData Pict(1), 5, NumSegments, ForeColor, False
  283.         
  284.     TransformData M3, 5, NumSegments
  285.     SetPoints 5, NumSegments
  286.     TransformData Projector, 5, NumSegments
  287.     DrawSomeData Pict(1), 5, NumSegments, ForeColor, False
  288.     TransformData M4, 5, NumSegments
  289.     SetPoints 5, NumSegments
  290.     TransformData Projector, 5, NumSegments
  291.     DrawSomeData Pict(1), 5, NumSegments, ForeColor, False
  292.     TransformAllData Identity
  293.     DrawSomeData Proj(1), 1, 3, vbRed, True
  294.     DrawSomeData Proj(1), 5, NumSegments, ForeColor, False
  295.     ' ***************
  296.     ' * Around line *
  297.     ' ***************
  298.     CreateData
  299.     TransformAllData Projector
  300.     DrawSomeData Pict(2), 1, 3, vbRed, True
  301.     DrawSomeData Pict(2), 4, 4, vbBlue, False
  302.     DrawSomeData Pict(2), 5, NumSegments, ForeColor, False
  303.         
  304.     TransformData M5, 5, NumSegments
  305.     SetPoints 5, NumSegments
  306.     TransformData Projector, 5, NumSegments
  307.     DrawSomeData Pict(2), 5, NumSegments, ForeColor, False
  308.     TransformAllData Identity
  309.     DrawSomeData Proj(2), 1, 3, vbRed, True
  310.     DrawSomeData Proj(2), 5, NumSegments, ForeColor, False
  311.     For i = 0 To 2
  312.         Pict(i).Refresh
  313.         Proj(i).Refresh
  314.     Next i
  315. End Sub
  316. Private Sub CreateData()
  317.     ' Start with no data.
  318.     NumSegments = 0
  319.     ' Create the axes.
  320.     MakeSegment 0, 0, 0, 5, 0, 0    ' X axis.
  321.     MakeSegment 0, 0, 0, 0, 5, 0    ' Y axis.
  322.     MakeSegment 0, 0, 0, 0, 0, 5    ' Z axis.
  323.     ' Create the line.
  324.     MakeSegment -2 * Vx, -2 * Vy, -2 * Vz, 2 * Vx, 2 * Vy, 2 * Vz
  325.     ' Create the object to reflect.
  326.     MakeSegment Px, Py, Pz, Px, Py - 1, Pz - 1
  327.     MakeSegment Px, Py - 1, Pz - 1, Px, Py - 1, Pz + 1
  328.     MakeSegment Px, Py - 1, Pz + 1, Px, Py, Pz
  329. End Sub
  330.